package de.roon.weighttracker.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	// All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "WeightTracker.db";
 
    // Contacts table name
    private static final String TABLE_WEIGHTS = "weights";
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_DATE = "DATE";
    private static final String KEY_WEIGHT = "weight";
    private static final String KEY_BMI = "bmi";
 
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_WEIGHTS_TABLE = "CREATE TABLE " + TABLE_WEIGHTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY NOT NULL,"
                + KEY_DATE + " DATE NOT NULL, "
        		+ KEY_WEIGHT + " DECIMAL(3,2) NOT NULL,"
        		+ KEY_BMI + " Decimal(4,2) NOT NULL " + ")";
        db.execSQL(CREATE_WEIGHTS_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_WEIGHTS);
 
        // Create tables again
        onCreate(db);
    }
    
    public void close(SQLiteDatabase db) {
		db.close();
	}
    
    //Writes new Weight into DB
    public void addWeight(Weights weight) {
        SQLiteDatabase db = this.getWritableDatabase();
     
        ContentValues values = new ContentValues();
        values.put(KEY_DATE, weight.getDate()); // Contact Name
        values.put(KEY_WEIGHT, weight.getWeight()); // Contact Phone Number
        values.put(KEY_BMI, weight.getBMI());
     
        // Inserting Row
        db.insert(TABLE_WEIGHTS, null, values);
        db.close(); // Closing database connection
    }
    
    //gets specific weight from DB
    public Weights getWeight(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
     
        Cursor cursor = db.query(TABLE_WEIGHTS, new String[] { KEY_ID,
                KEY_DATE, KEY_WEIGHT, KEY_BMI }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
     
        Weights weight = new Weights(Integer.parseInt(cursor.getString(0)),
                cursor.getDouble(1), cursor.getDouble(2), cursor.getDouble(3));
        db.close();
        // return contact
        return weight;
    }
    
    //get all weights
    public List<Weights> getAllWeights() {
        List<Weights> weightList = new ArrayList<Weights>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_WEIGHTS;
     
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
     
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Weights weight = new Weights();
                weight.setID(Integer.parseInt(cursor.getString(0)));
                weight.setDate(cursor.getDouble(1));
                weight.setWeight(cursor.getDouble(2));
                weight.setBMI(cursor.getDouble(3));
                // Adding contact to list
                weightList.add(weight);
            } while (cursor.moveToNext());
        }
     
        // return contact list
        return weightList;
    }
    
    // Updating single weight
    public int updateContact(Weights weight) {
	    SQLiteDatabase db = this.getWritableDatabase();
	 
	    ContentValues values = new ContentValues();
	    values.put(KEY_DATE, weight.getDate());
	    values.put(KEY_WEIGHT, weight.getWeight());
	    values.put(KEY_BMI, weight.getBMI());
	 
	    // updating row
	    return db.update(TABLE_WEIGHTS, values, KEY_ID + " = ?",
	    new String[] { String.valueOf(weight.getID()) });
    }
    
    // Deleting single weight
    public void deleteContact(Weights weight) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_WEIGHTS, KEY_ID + " = ?",
                new String[] { String.valueOf(weight.getID()) });
        db.close();
    }
    
}
